package com.tianqu.tms.www.statistics.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsStatisticsAjaxAction;
import com.tianqu.tms.www.api.network.PSHTTPUtil;
import com.tianqu.tms.www.statistics.dto.CarAgeLibraryDto;
import com.tianqu.tms.www.statistics.dto.CarCartrainDto;
import com.tianqu.tms.www.statistics.dto.CarPriceLibraryDto;
import com.tianqu.tms.www.statistics.dto.CarVendorDto;
import com.tianqu.tms.www.statistics.dto.InventoryVehicleDto;
import com.tianqu.tms.www.statistics.service.InventoryCarAgeService;
import com.tianqu.tms.www.statistics.service.InventoryCarCartrainService;
import com.tianqu.tms.www.statistics.service.InventoryCarPriceService;
import com.tianqu.tms.www.statistics.service.InventoryCarVendorService;
import com.tianqu.tms.www.statistics.service.InventoryVehicleService;

/**
 * 库存车辆
 * @author dkt
 *
 */
public class InventoryVehicleController extends TmsStatisticsAjaxAction {

	private Logger LOGGER = Logger.getLogger(InventoryVehicleController.class);
	private InventoryVehicleService inventoryVehicleService = new InventoryVehicleService();
	private InventoryCarAgeService inventoryCarAgeService = new InventoryCarAgeService();
	private InventoryCarPriceService inventoryCarPriceService = new InventoryCarPriceService();
	private InventoryCarVendorService inventoryCarVendorService = new InventoryCarVendorService();
	private InventoryCarCartrainService inventoryCarCartrainService = new InventoryCarCartrainService();

	/**
	 * 累计统计
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCumulativeStatisticalInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getCumulativeStatisticalInfo();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}
		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 实时统计
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getRealTimeStatisticalInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getRealTimeStatisticalInfo();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 今天/当月新增库存数量、当月新增同比、当月新增环比
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getNewInventoryNumber(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getNewInventoryNumber();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 品牌TOP5
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getBrandTop5(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getBrandTop5();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 车系TOP5
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCarStyleTop5(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getCarStyleTop5();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 库存平均车价商户TOP5
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAverageCarPricesForMerchantsTop5(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getAverageCarPricesForMerchantsTop5();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 库存量商户TOP5
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getInventoryTop5(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getInventoryTop5();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 库存车辆价格分布
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getInventoryCarsPrice(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getInventoryCarsPrice();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 库存车龄分布
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getInventoryCarsAge(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.getInventoryCarsAge();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}

	/**
	 * 近10个月内新增入库走势图/最近10个月内车位使用率走势图
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward get10MonthNewStorageAndParkingRate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";

		try {
			InventoryVehicleDto inventoryVehicleDto = inventoryVehicleService.get10MonthNewStorageAndParkingRate();
			json = PSHTTPUtil.convertJavaBeanToJson(inventoryVehicleDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}
	
	/**
	 * 车龄统计-在库
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCarAgeInLibrary(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		try {
			// TODO:车龄统计-在库接口OK，前台检索条件以yyyy-MM-dd的格式检索，且保证startDate < endDate
			CarAgeLibraryDto carAgeLibraryDto = inventoryCarAgeService.getCarAgeLibrary(startDate, endDate);
			json = PSHTTPUtil.convertJavaBeanToJson(carAgeLibraryDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}
	
	/**
	 * 车价统计-在库
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCarPriceInLibrary(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		try {
			// TODO:车价统计-在库接口OK，前台检索条件以yyyy-MM-dd的格式检索，且保证startDate < endDate
			CarPriceLibraryDto carPriceLibraryDto = inventoryCarPriceService.getCarPriceLibrary(startDate, endDate);
			json = PSHTTPUtil.convertJavaBeanToJson(carPriceLibraryDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		LOGGER.debug("param:"+json +"   result:"+json);
		responseValues(response, json, JSON);
		return null;
	}
	
	/**
	 * 品牌统计-在库
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCarVendorInLibrary(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		try {
			// TODO:品牌统计-在库接口OK，前台检索条件以yyyy-MM-dd的格式检索，且保证startDate < endDate
			CarVendorDto carVendorDto = inventoryCarVendorService.getCarVendorLibrary(startDate, endDate);
			json = PSHTTPUtil.convertJavaBeanToJson(carVendorDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		System.err.println(json);
		LOGGER.debug("param:"+json +"   result:"+json);
		
		responseValues(response, json, JSON);
		return null;
	}
	
	/**
	 * 车系统计-在库
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCarCartrainInLibrary(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String json = "";
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		try {
			// TODO:车系统计-在库接口OK，前台检索条件以yyyy-MM-dd的格式检索，且保证startDate < endDate
			CarCartrainDto carCartrainDto = inventoryCarCartrainService.getCarCartrainLibrary(startDate, endDate);
			json = PSHTTPUtil.convertJavaBeanToJson(carCartrainDto);
		} catch (Exception e) {
			LOGGER.error("InventoryVehicleController web api error",e);
		}

		System.err.println(json);
		LOGGER.debug("param:"+json +"   result:"+json);
		
		responseValues(response, json, JSON);
		return null;
	}
}
